package com.dianning.service.imp;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import org.apache.commons.logging.*;

import com.dianning.bean.AdSpace;
import com.dianning.bean.Area;
import com.dianning.bean.Visitor;
import com.dianning.comm.Constant;
import com.dianning.dao.AdAdsDAO;
import com.dianning.dao.AdSpaceDAO;
import com.dianning.dao.VisitorDAO;
import com.dianning.exception.DianningException;
import com.dianning.service.AdService;
import com.dianning.service.Cache;
import com.dianning.service.VisitorService;

public class AdServiceImp implements AdService {

	private static final Log logger = LogFactory.getLog(AdServiceImp.class);
	
	private AdAdsDAO adAdsDAO;
	private AdSpaceDAO adSpaceDAO;
	private Cache adCache;

	public AdAdsDAO getAdAdsDAO() {
		return adAdsDAO;
	}

	public void setAdAdsDAO(AdAdsDAO adAdsDAO) {
		this.adAdsDAO = adAdsDAO;
	}

	public AdSpaceDAO getAdSpaceDAO() {
		return adSpaceDAO;
	}

	public void setAdSpaceDAO(AdSpaceDAO adSpaceDAO) {
		this.adSpaceDAO = adSpaceDAO;
	}

	public Cache getAdCache() {
		return adCache;
	}

	public void setAdCache(Cache adCache) {
		this.adCache = adCache;
	}

	/**
	 * 根据code获取Ad对象列表
	 * 
	 * @param code
	 * 			广告位代码
	 * @return List
	 * 			Ad对象
	 */
	public List findAdsByCode(String code,int endTime, String orderby, int ascOrDesc){
		AdSpace as = null;
		if(Constant.USE_CACHE){
			as = (AdSpace)this.getAdCache().get("[adSpace]["+code+"]");
			if(as == null){
				as = this.getAdSpaceDAO().findAdSpaceByCode(code);
				this.getAdCache().add("[adSpace]["+code+"]",as);
			}
		}else{
			as = this.getAdSpaceDAO().findAdSpaceByCode(code);
		}
		if(as == null){
			return null;
		}else{
			if(Constant.USE_CACHE){
				List  l = (List)this.getAdCache().get("[ad]["+as.getId()+"]["+endTime+"]["+orderby+"]["+ascOrDesc+"]");
				if(l == null){
					l = this.getAdAdsDAO().findAdAds(as.getId(), endTime, orderby, ascOrDesc);
					this.getAdCache().add("[ad]["+as.getId()+"]["+endTime+"]["+orderby+"]["+ascOrDesc+"]",l);
				}
				return l;
			}else{
				return this.getAdAdsDAO().findAdAds(as.getId(), endTime, orderby, ascOrDesc);
			}
		}
	}

}
